
import { createRouter, createWebHistory } from 'vue-router';
import { routes } from '@/router/routes';

const router = createRouter({
  history: createWebHistory(),
  routes,
  scrollBehavior() {
    return { top: 0, left: 0 }
  },
})

// 路由守卫
router.beforeEach((to, from, next) => {
  let userInfo = JSON.parse(localStorage.getItem('userInfo') as string) || {}
  if (to.path === '/login') {
    userInfo.phone?next('/'):next()
  } else {
    if (to.path === '/center' || to.path === '/shopCart') {
      userInfo.phone?next():next('/login')
    } else {
      next()
    }
  }
})

// 导出路由
export default router;